#!/bin/bash

echo "========================================"
echo "  TimeShare 项目发布到 Gitee"
echo "========================================"
echo ""

# 检查是否已经初始化Git
if [ ! -d ".git" ]; then
    echo "[1/6] 正在初始化Git仓库..."
    git init
    echo "✓ Git仓库初始化完成"
    echo ""
else
    echo "[1/6] Git仓库已存在，跳过初始化"
    echo ""
fi

echo "[2/6] 请输入您的Gitee用户信息（如果已配置可直接回车跳过）"
echo ""
read -p "请输入您的姓名: " gitname
if [ -n "$gitname" ]; then
    git config user.name "$gitname"
    echo "✓ 已设置用户名: $gitname"
fi

read -p "请输入您的邮箱: " gitemail
if [ -n "$gitemail" ]; then
    git config user.email "$gitemail"
    echo "✓ 已设置邮箱: $gitemail"
fi
echo ""

echo "[3/6] 请输入您的Gitee仓库地址"
echo "示例: https://gitee.com/your-username/timeshare.git"
read -p "仓库地址: " giturl

if [ -z "$giturl" ]; then
    echo "✗ 错误：仓库地址不能为空！"
    exit 1
fi

echo ""
echo "[4/6] 正在添加文件到暂存区..."
git add .
echo "✓ 文件添加完成"
echo ""

echo "[5/6] 正在提交到本地仓库..."
git commit -m "feat: 初始化TimeShare项目 - 校园技能互助平台"
if [ $? -eq 0 ]; then
    echo "✓ 提交完成"
else
    echo "⚠ 提交失败或没有需要提交的更改"
fi
echo ""

echo "[6/6] 正在配置远程仓库并推送..."
git remote remove origin 2>/dev/null
git remote add origin "$giturl"
echo "✓ 远程仓库配置完成"
echo ""

echo "正在推送到Gitee..."
git branch -M main
git push -u origin main

if [ $? -eq 0 ]; then
    echo ""
    echo "========================================"
    echo "  ✓ 恭喜！项目已成功发布到Gitee！"
    echo "========================================"
    echo ""
    echo "您的项目地址："
    echo "${giturl%.git}"
    echo ""
    echo "后续更新代码请使用："
    echo "  git add ."
    echo "  git commit -m \"提交说明\""
    echo "  git push"
    echo ""
else
    echo ""
    echo "========================================"
    echo "  ✗ 推送失败，请检查："
    echo "========================================"
    echo "  1. 网络连接是否正常"
    echo "  2. Gitee仓库地址是否正确"
    echo "  3. 是否有推送权限"
    echo ""
    echo "您可以手动执行：git push -u origin main"
    echo ""
fi

